package cn.golxen.blog.config;

import cn.golxen.blog.filter.BlogFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WebConfig {
    @Bean
    public FilterRegistrationBean<BlogFilter> firstFilter(){
        FilterRegistrationBean<BlogFilter> filterRegBean=new FilterRegistrationBean<>();
        filterRegBean.setFilter(new BlogFilter());
        filterRegBean.addUrlPatterns("/admin/update/*");
        filterRegBean.addUrlPatterns("/category/admin/*");
        filterRegBean.addUrlPatterns("/draft/*");
        filterRegBean.addUrlPatterns("/essay/admin/*");
        filterRegBean.addUrlPatterns("/label/admin/*");
        filterRegBean.setOrder(1);
//        filterRegBean.addInitParameter("checkUri","/admin/check");
        return filterRegBean;
    }

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置你要允许的网站域名
        config.addAllowedOrigin("*");
//        config.addAllowedOrigin("http://localhost:8080");
//        config.addAllowedOrigin("https://127.0.0.1:9001");
//        config.addAllowedOrigin("https://blog.golxen.com");
//        config.addAllowedOrigin("https://blog.golxen.com:443");
//        config.addAllowedOrigin("https://blog.golxen.com:9001");
        //允许跨域发送cookie
//        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
